﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WX.WMS.DataModel;
using WX.WMS.Common;

namespace WX.WMS.BLL
{
    public class FloorBLL : BaseBLL
    {
        private BaseDAO DAO = null;
        public FloorBLL()
        {
            DAO = new BaseDAO();
        }

        public bool IsExisted(string AreaID, string Name)
        {
            if (DAO.T_FLOORs.Where(t => t.AREA_ID.ToString() == AreaID && t.FloorName.ToLower() == Name).Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public IEnumerable<T_FLOOR> GetFloorList()
        {
            var list = DAO.T_FLOORs.Where(WhereCondition);

            if (OrderCondition != "")
            {
               list= list.OrderBy(OrderCondition);
            }
            else
            {
              list= list.OrderBy(t => t.FloorName);
            }

            return list;
        }

        public T_FLOOR GetFloorByID(string ID)
        {
            return DAO.T_FLOORs.Where(t => t.FloorID.ToString() == ID).SingleOrDefault();
        }

        public int AddFloor(T_FLOOR Floor)
        {
            try
            {
                Floor.FloorID = DAO.GetGUID();

                DAO.T_FLOORs.InsertOnSubmit(Floor);
                DAO.SubmitChanges();

                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int AddFloor(T_FLOOR Floor, out string FloorGuid)
        {
            try
            {
                Floor.FloorID = DAO.GetGUID();
                
                DAO.T_FLOORs.InsertOnSubmit(Floor);
                DAO.SubmitChanges();

                FloorGuid = Floor.FloorID.ToString();
                return 1;
            }
            catch (Exception ex)
            {
                FloorGuid = "";
                return -1;
            }
        }

        public int UpdateFloor(T_FLOOR Floor)
        {
            try
            {
                T_FLOOR updateEntity = DAO.T_FLOORs.SingleOrDefault(t=>t.FloorID.ToString()==Floor.FloorID.ToString());
                if (updateEntity == null) 
                    return -1;

                updateEntity.FloorName = Floor.FloorName;
                updateEntity.AREA_ID = Floor.AREA_ID;
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        
        public int DeleteFloor(string ID)
        {
            try
            {
                T_FLOOR deleteEntity = DAO.T_FLOORs.SingleOrDefault(t => t.FloorID.ToString() == ID);
                if (deleteEntity == null) 
                    return -1;
                DAO.T_FLOORs.DeleteOnSubmit(deleteEntity);
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
    }
}
